package com.huawei.appmarket.framework.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Gallery;
import java.lang.reflect.Field;
import o.Logger;
import o.MinimumFactory;

public class BannerGallery extends Gallery {
    private c ʻ;
    private boolean ˊ = false;
    private boolean ˋ = false;
    private boolean ˎ = true;
    private final Handler ˏ = new Handler();
    private int ॱ = 0;
    private final Runnable ॱॱ = new Runnable(this) {
        final /* synthetic */ BannerGallery ˊ;

        {
            this.ˊ = r1;
        }

        public void run() {
            this.ˊ.ˋ = false;
            this.ˊ.setSoundEffectsEnabled(false);
            if (this.ˊ.ˎ()) {
                this.ˊ.setSoundEffectsEnabled(true);
                this.ˊ.ॱ(this);
            }
        }
    };
    private boolean ᐝ = false;

    public interface c {
        void ˋ();

        void ॱ();
    }

    public BannerGallery(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.ᐝ = MinimumFactory.getBoolValue(context);
        ˋ();
    }

    public void setMotionEventer(c cVar) {
        this.ʻ = cVar;
    }

    public void setSupportImmerse(boolean z) {
        this.ˊ = z;
    }

    private void ˏ() {
        this.ˎ = true;
        this.ˋ = false;
        this.ˏ.removeCallbacks(this.ॱॱ);
    }

    private void ॱ() {
        this.ˎ = false;
        ॱ(this.ॱॱ);
    }

    public void ˏ(int i) {
        this.ॱ = i;
        if (i <= 1) {
            this.ˎ = true;
        } else if (!this.ˋ) {
            ॱ();
        }
    }

    private void ॱ(Runnable runnable) {
        if (!this.ˋ) {
            this.ˋ = true;
            this.ˏ.postDelayed(runnable, 5000);
        }
    }

    private boolean ˊ() {
        return hasWindowFocus() && getVisibility() == 0 && isShown();
    }

    private boolean ˎ() {
        if (this.ˎ || this.ॱ <= 1 || !ˊ()) {
            return false;
        }
        try {
            if (getSelectedItemPosition() + 1 >= this.ॱ) {
                setSelection(0);
            } else if (this.ᐝ) {
                onScroll(null, null, -2.0f, 0.0f);
                onKeyDown(21, null);
            } else {
                onScroll(null, null, 2.0f, 0.0f);
                onKeyDown(22, null);
            }
        } catch (IllegalArgumentException e) {
            Logger.ˋ("BannerGallery", "transNextItem() " + e.toString());
        }
        return true;
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == 0 || motionEvent.getAction() == 2) {
            ˏ();
            if (this.ˊ && this.ʻ != null) {
                this.ʻ.ˋ();
            }
        } else if (motionEvent.getAction() == 1) {
            this.ˋ = false;
            ॱ();
            if (this.ˊ && this.ʻ != null) {
                this.ʻ.ॱ();
            }
        }
        return super.onTouchEvent(motionEvent);
    }

    private void ˋ() {
        float f = getResources().getDisplayMetrics().density;
        Class superclass = getClass().getSuperclass();
        try {
            Field declaredField = superclass.getDeclaredField("mFlingDistance");
            declaredField.setAccessible(true);
            declaredField.setInt(this, (int) (25.0f * f));
            Field declaredField2 = superclass.getDeclaredField("mMinimumVelocity");
            declaredField2.setAccessible(true);
            declaredField2.setInt(this, (int) (f * 40.0f));
        } catch (Exception e) {
            Logger.ˋ("BannerGallery", "field error : " + e.toString());
        }
    }

    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        int i;
        if (motionEvent2.getX() > motionEvent.getX()) {
            i = 21;
        } else {
            i = 22;
        }
        onKeyDown(i, null);
        return true;
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        ॱ();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        ˏ();
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        setSoundEffectsEnabled(false);
        boolean onKeyDown = super.onKeyDown(i, keyEvent);
        setSoundEffectsEnabled(true);
        return onKeyDown;
    }

    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (i == 0) {
            ॱ();
        } else {
            ˏ();
        }
    }
}
